clear all
set more off
capture log close
log using "$output/figureA4.log", replace

version 16	

use "$data/ctctaxsim_mar1kid1.dta", clear
append using "$data/ctctaxsim_mar1kid2.dta"
append using "$data/ctctaxsim_mar1kid3.dta"
tab depx

sum fiitax earnings fedtaxlessctc v25 totctc
replace earnings=earnings*1000
replace ctc=ctc*1000
replace v25=v25*1000
replace totctc=totctc*1000
sum fiitax earnings fedtaxlessctc v25 totctc

gen avgtaxrate=fiitax/earnings
gen avgtaxrate2021=fedtaxlessctc/earnings	/* Don't include new CTC */

gen avgtaxeitc=-v25/earnings
gen avgtaxctc=-totctc/earnings
gen avgtaxctc2021=0

gen avgtaxeitcctc=avgtaxeitc+avgtaxctc

*br avgtaxrate avgtaxeitcctc avgtaxeitc avgtaxctc earnings year depx if year==2018
*br avgtaxrate avgtaxeitcctc avgtaxeitc avgtaxctc earnings year depx avginctax if year==2017
gen avginctax=avgtaxrate- avgtaxeitcctc

tab year avginctax if earnings==40000 & depx==1
replace avginctax=avginctax*.04/.037875 if year==2017
tab year avginctax if earnings==40000 & depx==1

foreach var in avgtaxeitc avgtaxctc avgtaxctc2021 avginctax{
	replace `var'=-`var'
}

foreach var in avgtaxeitc avgtaxctc avgtaxctc2021 avginctax{
	replace `var'=`var'+1
}


twoway line avgtaxeitc earnings if year==2017 & depx==1, pstyle(p1) || line avgtaxctc earnings if year==2017 & depx==1, lp(_) pstyle(p2) || line avgtaxctc earnings if year==2018 & depx==1, lp(-) pstyle(p2) || line avgtaxctc2021 earnings if year==2018 & depx==1, lw(thick) pstyle(p2) || line avginctax earnings if year==2017 & depx==1, pstyle(p3) || line avginctax earnings if year==2018 & depx==1, pstyle(p3) lp(-) graphregion(color(white)) bgcolor(white) leg(off) ytitle("Return to Work") xtitle("Earnings" "{bf:Panel A: One Child}") text(1.4 50000 "Families with 1 Kid" , j(left) size(medsmall)) ylabel(.75(.25)1.5)
graph save "$output/avgtaxdecomp_mar1kid1.gph", replace

twoway line avgtaxeitc earnings if year==2017 & depx==1, pstyle(p1) || line avgtaxctc earnings if year==2017 & depx==2, lp(_) pstyle(p2) || line avgtaxctc earnings if year==2018 & depx==2, lp(-) pstyle(p2) || line avgtaxctc2021 earnings if year==2018 & depx==2,  lw(thick) pstyle(p2) || line avginctax earnings if year==2017 & depx==2, pstyle(p3) || line avginctax earnings if year==2018 & depx==2, pstyle(p3) lp(-) graphregion(color(white)) bgcolor(white) leg(off) ytitle("Return to Work") xtitle("Earnings" "{bf:Panel B: Two Children}") text(1.4 50000 "Families with 2 Kids" , j(left) size(medsmall)) ylabel(.75(.25)1.5)
graph save "$output/avgtaxdecomp_mar1kid2.gph", replace

twoway line avgtaxeitc earnings if year==2017 & depx==3, pstyle(p1) || line avgtaxctc earnings if year==2017 & depx==3, lp(_) pstyle(p2) || line avgtaxctc earnings if year==2018 & depx==3, lp(-) pstyle(p2) || line avgtaxctc2021 earnings if year==2018 & depx==3,  lw(thick) pstyle(p2) || line avginctax earnings if year==2017 & depx==3, pstyle(p3) || line avginctax earnings if year==2018 & depx==3, pstyle(p3) lp(-) graphregion(color(white)) bgcolor(white) leg(off) ytitle("Return to Work") xtitle("Earnings" "{bf:Panel C: Three Children}") text(1.4 50000 "Families with 3 Kids" , j(left) size(medsmall)) ylabel(.75(.25)1.5)
graph save "$output/avgtaxdecomp_mar1kid3.gph", replace

*** I just use this one to grab the legend and copy into the main figure
twoway line avgtaxeitc earnings if year==2017 & depx==3, pstyle(p1) || line avgtaxctc earnings if year==2017 & depx==3, lp(_) pstyle(p2) || line avgtaxctc earnings if year==2018 & depx==3, lp(-) pstyle(p2) || line avgtaxctc2021 earnings if year==2018 & depx==3,  lw(thick) pstyle(p2) || line avginctax earnings if year==2017 & depx==3, pstyle(p3) || line avginctax earnings if year==2018 & depx==3, pstyle(p3) lp(-) graphregion(color(white)) bgcolor(white) leg(order(1 "EITC" 2 "CTC 2017" 3 "CTC 2020" 4 "CTC 2021" 5 "Income Tax 2017" 6 "Income Tax 2020") c(1) pos(5) ring(0)) ytitle("Average Tax Rate") xtitle("Earnings" "{bf:Panel C}") text(.15 50000 "Families with 3 Kids" , j(left) size(medsmall)) ylabel(-.5(.25).25)
graph save "$output/forlegend.gph", replace

graph combine "$output/avgtaxdecomp_mar1kid1.gph" "$output/avgtaxdecomp_mar1kid2.gph" "$output/avgtaxdecomp_mar1kid3.gph",graphregion(color(white)) 
graph export "$output/figureA4.png", replace

erase "$output/avgtaxdecomp_mar1kid1.gph"
erase "$output/avgtaxdecomp_mar1kid2.gph"
erase "$output/avgtaxdecomp_mar1kid3.gph"

log close